package com.twq.patternmatch.extractors

import com.twq.patternmatch.basic._
import com.twq.patternmatch.pattern._

/**
 * Created by tangweiqun on 2017/11/22.
 */
object ExtractorVsCaseClass {
  def main(args: Array[String]): Unit = {
    //1、我们使用case class
    //假设需要修改case class则会影响到客户端的代码
    simplifyTop(UnOp("-", UnOp("-", Var("x"))))

    //2、我们把Email变成Extractor，修改Extractor里的逻辑不会影响客户端的代码
    val email = "laotang@qq.com"
    email match {
      case EMail(user, domain) => println(user + " @ " + domain)
      case _ => println("not an email address")
    }
  }

  def simplifyTop(expr: Expr): Expr = expr match {
    case UnOp("-", UnOp("-", e)) if (e == "x") => e // Double negation
    case BinOp("+", e, Number(0)) => e // Adding zero
    case BinOp("*", e, Number(1)) => e // Multiplying by one
    case _ => expr
  }
}


